package com.example.smartcity_40.Fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import com.example.smartcity_40.R
import com.example.smartcity_40.databinding.FragmentMeBinding
import com.example.smartcity_40.me.*
import com.example.smartcity_40.tool.g
import com.example.smartcity_40.tool.jump
import com.example.smartcity_40.tool.tool
import com.example.smartcity_40.tool.viewBinding
import com.google.android.material.button.MaterialButton

class MeFragment : Fragment() {
    private val vb by viewBinding(FragmentMeBinding::inflate)
    val TAG = "MeFragment"
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        initView()
        return vb.root
    }

    private fun initView() {
        val close = {
            Toast.makeText(context,"未登录",Toast.LENGTH_SHORT).show()
        }
        tool.checkToken {
            if (it) {
                tool.send("/prod-api/api/common/user/getInfo","GET",null,true) {
                    try {
                        val data = g.fromJson(it,UserInfoBean::class.java).user
                        vb.meUser.text = data.nickName
                    } catch (e: Exception) {
                        Log.e(TAG, "initView:${e.message} ")
                    }
                    vb.meUserInfo.setOnClickListener {
                        jump(UserInfoActivity::class.java)
                    }
                    vb.mePassword.setOnClickListener {
                        jump(PasswordActivity::class.java)
                    }
                    vb.meFeedback.setOnClickListener {
                        jump(FeedbackActivity::class.java)
                    }
                    vb.meOrder.setOnClickListener {
                        jump(OrderActivity::class.java)
                    }
                    vb.meBtn.text = "退出登录"
                    vb.meBtn.setOnClickListener {
//                          删除token
                        tool.set("token","")
//                            再次调用方法执行未登录逻辑 改变按钮文字 让功能模块无法点击
                        initView()
                    }
                }
            } else {
                vb.meBtn.text = "未登录"
                vb.meUser.text = "用户名"
                vb.meAvatar.setImageResource(R.drawable.waimai)
                for (i in 0 until vb.meList.childCount) {
                    val view = vb.meList.getChildAt(i)
                    if (view is RelativeLayout) {
                        view.setOnClickListener {
                            close()
                        }
                    } else {
                        (view as MaterialButton).text = "登录"
                        view.setOnClickListener {
                            context?.startActivity(Intent(context,LoginActivity::class.java))
                        }
                    }
                }
            }
        }
    }

    override fun onResume() {
        initView()
        super.onResume()
    }
}